from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import pymysql
import openpyxl


class ReadExcel:
    @staticmethod
    def read_excel1(filename, sheetname):
        try:
            wb = openpyxl.load_workbook(filename)
            sheet = wb[sheetname]
            data = [[sheet.cell(row=i, column=j).value for j in range(1, sheet.max_column + 1)] for i in range(1, sheet.max_row + 1)]
            return data
        except Exception as e:
            print('数据读取异常', e)
#登陆成功


class DButiles:
    def __init__(self, host, user, password, database, port=3306, charset='utf8'):
        self.host = host
        self.user = user
        self.password = password
        self.database = database
        self.port = port
        self.charset = charset

    def get_conn(self):
        conn = pymysql.connect(host=self.host, user=self.user, password=self.password, database=self.database,
                               port=self.port, charset=self.charset)
        return conn

    def select(self, sql, data=None):
        try:
            db = self.get_conn()
            cursor = db.cursor()
            cursor.execute(sql, data)
            result = cursor.fetchall()
            return result
        except Exception as e:
            return '查询失败', e



# def assert1(result, expect):
#     result = driver


def assert1(result, expect, driver):
    if result == expect:
        driver.save_screenshot('success.png')
        print('测试成功')
    else:
        driver.save_screenshot('error.png')
    assert result == expect


def assert2(result, result1, driver):
    if result in result1:
        # driver.save_screenshot('success.png')
        print('测试成功')
    else:
        driver.save_screenshot('error.png')
    assert result in result1

def assert3(data, datac, driver):
    if len(datac) == len(data)+1:
        driver.save_screenshot('success.png')
        print('测试成功')
    else:
        driver.save_screenshot('error.png')
    assert len(datac) == len(data)+1


def login_s(user, pwd, driver):
    driver.find_element(By.ID, 'username').send_keys(user)
    time.sleep(2)
    driver.find_element(By.ID, 'password').send_keys(pwd)
    time.sleep(2)
    driver.find_element(By.ID, 'login_btn').click()
    time.sleep(2)
    return driver.title




    # driver.quit()
# test_login_s()
#登陆失败
# driver = webdriver.Chrome()
#
# driver.get('http://localhost:90/')
#
# driver.maximize_window()
#


# def login_f(user, pwd):
#
#     driver.find_element(By.ID, 'username').send_keys(user)
#     time.sleep(2)
#     driver.find_element(By.ID, 'password').send_keys(pwd)
#     time.sleep(2)
#     driver.find_element(By.ID, 'login_btn').click()
#
#     re = driver.title
#     excep = '度小满理财-登录[内测版]'
#     if re == excep:
#         print('测试成功')
#         driver.save_screenshot('error.png')
#     else:
#         print('测试失败')
#         driver.save_screenshot('error.png')
#     time.sleep(5)
#     assert re == excep
#     driver.quit()

# 注册成功
# driver = webdriver.Chrome()
#
# driver.get('http://localhost:90/')
#
# driver.maximize_window()
#


def register_s(user, pwd, pwd1, driver):

    driver.find_element(By.LINK_TEXT, '还没有账号？去注册').click()
    time.sleep(2)
    driver.find_element(By.ID, 'username').send_keys(user)
    time.sleep(2)
    driver.find_element(By.ID, 'password').send_keys(pwd)
    time.sleep(2)
    driver.find_element(By.ID, 'repassword').send_keys(pwd1)
    time.sleep(2)
    driver.find_element(By.XPATH, '/html/body/div/div/div/div[1]/h1').click()
    time.sleep(2)
    driver.find_element(By.ID, 'login_btn').click()
    time.sleep(5)
    return driver.title
    # excep = '度小满理财-登录[内测版]'
    # if re == excep:
    #     driver.save_screenshot('success1.png')
    #     print('测试成功')
    # else:
    #     print('测试失败')
    # time.sleep(5)
    # assert re == excep
    # driver.quit()


# test_register_s()

#注册失败
# driver = webdriver.Chrome()
#
# driver.get('http://localhost:90/')
#
# driver.maximize_window()


def register_f(user, pwd, pwd1, driver):
    time.sleep(2)
    driver.find_element(By.LINK_TEXT, '还没有账号？去注册').click()
    time.sleep(2)
    driver.find_element(By.ID, 'username').send_keys(user)
    time.sleep(2)
    driver.find_element(By.ID, 'password').send_keys(pwd)
    time.sleep(2)
    driver.find_element(By.ID, 'repassword').send_keys(pwd1)
    time.sleep(2)
    driver.find_element(By.XPATH, '/html/body/div/div/div/div[1]/h1').click()
    time.sleep(2)
    driver.find_element(By.ID, 'login_btn').click()
    # time.sleep(5)
    return driver.title
    # excep = '个人理财系统注册界面'
    # if re == excep:
    #     # driver.save_screenshot('success1.png')
    #     print('测试成功')
    #     driver.save_screenshot('error1.png')
    # else:
    #     print('测试失败')
    # time.sleep(5)
    # assert re == excep
    # driver.quit()

#网贷成功
# driver = webdriver.Chrome()
#
# driver.get('http://localhost:90/')
#
# driver.maximize_window()
#
#




def loan_s(user, pwd, money, day, driver):

    driver.find_element(By.ID, 'username').send_keys(user)
    time.sleep(2)
    driver.find_element(By.ID, 'password').send_keys(pwd)
    time.sleep(2)
    driver.find_element(By.ID, 'login_btn').click()
    driver.implicitly_wait(5)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
    time.sleep(2)
    driver.find_element(By.ID, 'amount').send_keys(money)
    time.sleep(2)
    driver.find_element(By.ID, 'term').send_keys(day)
    time.sleep(2)
    driver.find_element(By.ID, 'submit').click()
    driver.implicitly_wait(5)
#     if driver.refresh():
#         print('申请成功')
# loan_s('丁真', 123456, 1000, 10)
    # driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
    # time.sleep(2)
    # alter = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div')
    # alter.click()
    # WebDriverWait(driver, 10).until(ec.visibility_of_element_located())
    # alert = driver.switch_to.alert
    # alert_text = alert.text
    # print(alert_text)
    # alert.accept()
    # driver.save_screenshot('success2.png')
    # print('申请成功')
    # assert True
    # driver.quit()
# test_loan_s()
#网贷失败
# driver = webdriver.Chrome()
#
# driver.get('http://localhost:90/')
#
# driver.maximize_window()
#
#


def loan_f(user, pwd, money, day, driver):

    driver.find_element(By.ID, 'username').send_keys(user)
    time.sleep(2)
    driver.find_element(By.ID, 'password').send_keys(pwd)
    time.sleep(2)
    driver.find_element(By.ID, 'login_btn').click()
    driver.implicitly_wait(5)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
    time.sleep(2)
    driver.find_element(By.ID, 'amount').send_keys(money)
    time.sleep(2)
    driver.find_element(By.ID, 'term').send_keys(day)
    time.sleep(2)
    driver.find_element(By.ID, 'submit').click()
    driver.implicitly_wait(5)
    driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
    return WebDriverWait(driver, 5).until(ec.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button'))).text
    # excep = '确认'
    # if re.text == excep:
    #     print('申请失败')
    # assert re.text == excep
    # # print(re)
    # # re.click()
    # # driver.save_screenshot('error2.png')
    # time.sleep(5)
    # driver.quit()


def fund_s(user, pwd, driver):

    driver.find_element(By.ID, 'username').send_keys(user)
    time.sleep(2)
    driver.find_element(By.ID, 'password').send_keys(pwd)
    time.sleep(2)
    driver.find_element(By.ID, 'login_btn').click()
    driver.implicitly_wait(5)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[3]/td[9]/button').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@class="layui-layer-content"]//*[@id="password"]').click()
    time.sleep(2)

    # element = WebDriverWait(driver,10,0.5).until(ec.presence_of_element_located((By.XPATH, '//*[@id="password"]')))
    # print(type(element))
    # print(element.text)
    # element.send_keys(666666)

    # try:
    #     # driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
    #     # driver.implicitly_wait(5)
    #     driver.find_element(By.XPATH, '//*[@class="layui-layer-content"]//*[@id="password"]').click()
    #     print('success')
    # except Exception as e:
    #     print('点击失败', e)
    # try:
    #     driver.find_element(By.ID, 'password').click()
    # except Exception as e:
    #     print('点击失败', e)
    driver.find_element(By.XPATH, '//*[@class="layui-layer-content"]//*[@id="password"]').send_keys(666666)
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
    time.sleep(2)
    return driver.find_element(By.XPATH, '//*[@class="jconfirm-content"]/div').text
    # print(re.text)
    # excep = '买入成功!'
    # if re.text == excep:
    #     driver.save_screenshot('success4.png')
    # else:
    #     print("买入失败")
    # assert re.text == excep
    # driver.close()


def add_bankcard(user, pwd, bank, bankcard, driver):

    driver.find_element(By.ID, 'username').send_keys(user)
    time.sleep(2)
    driver.find_element(By.ID, 'password').send_keys(pwd)
    time.sleep(2)
    driver.find_element(By.ID, 'login_btn').click()
    driver.implicitly_wait(5)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
    time.sleep(2)
    driver.find_element(By.ID, 'bankCard_add_modal_btn').click()
    time.sleep(2)
    driver.find_element(By.ID, 'cardBank_add_input').send_keys(bank)
    time.sleep(2)
    driver.find_element(By.ID, 'cardNum_add_input').send_keys(bankcard)
    time.sleep(2)
    driver.find_element(By.ID, 'bankCard_save_btn').click()
    time.sleep(0.2)
    driver.save_screenshot('success5.png')
    time.sleep(2)
    return bankcard
    # print(re)
    # if '62222222222222222' in re:
    #     print('申请成功')
    # assert '卡号：62222222222222222' == re
    # driver.close()
